<?php
/**
 * 
 * @author tangbl
 * 
 */
class SiteAction extends CommonAction {
	
	public function _initialize() {
		parent::_initialize();
		parent::_initmenu();	
	}
	
	/**
	 * 常用地址列表
	 */
	public function index(){
	
		$company = $this->_get("company","intval",0);
		if(IS_AJAX){
	
			$desc    = $this->_get("orderDirection","trim","desc");;
			$order   = $this->_get("orderName","trim","addtime");
			$pageNum = $this->_get("pageNum","intval",C('PAGE_NUMS'));
			$nowPage = $this->_get("p","intval",1);
				
			$map   = array("company"=>$company, "pageNum"=>$pageNum, "p"=>$nowPage, "orderDirection"=>$desc ,"orderName"=>$order);
			$json  = sendRest("BDC/Outlet/page", $map);
			$data  = $json['data'];
			$count = $data['count'];
	
			$totalPage = ceil($count/$pageNum);
			$jsonData  = array('page'=>$nowPage,'total'=>$count,'totalPage'=>$totalPage);
			$upd       = L('cfg_list_itf_btn_modify');
			$del       = L('cfg_list_itf_btn_delete');
			
			foreach($data['list'] AS $row){
				$entry = array('id'=>$row['id'],
					'cell'=>array(
						'id'       => $row['id'],
						'title'    => $row['title'],
						'tel'      => $row['tel'],
						'mobile'   => $row['mobile'],
						'email'    => $row['email'],
						'addtime'  => date("Y-m-d",$row['addtime']),
						'deal'     => '
			 		<a href="'.__URL__.'/outletEdit/id/'.$row['id'].'" class="btn blue icn-only" title="'.$upd.'"><i class="icon-edit"></i></a>
		    	    <a href="#" class="btn red icn-only delAlone" title="'.$del.'" data-id="'.$row['id'].'"><i class="icon-remove"></i></a>'
					),
				);
				$jsonData['rows'][] = $entry;
			}
			$this->ajaxReturn($jsonData,"JSON");
		}else{
				
			$model   = D("Company");
			$detail  = $model->search($company);
			$this->assign("company",$detail);
				
			$this->display();
		}
	}
	
	/**
	 * 添加常用地址
	 */
	public function add(){
	
		if(IS_POST){
				
			$json = sendRest("BDC/Outlet/add", $_POST, "POST");
			if($json['status'] == 1){
				wlog(1);
				$this->assign("jumpUrl",U('Site/index'));
				$this->success(L('cfg_form_tipmsg_addsucc'));
			}else{
				wlog(2,$json['info']);
				$this->error($json['info']);
			}
		}else{
			
			//地址列表
			$data = sendRest("BDC/Outlet/dictAddress");
			$this->assign("dictAddressCN",$data['data']['zh-cn']['array']);
			$this->assign("dictAddressEN",$data['data']['en-us']['array']);
			
			$this->display();
		}
	}
	
	/**
	 * 修改常用地址
	 */
	public function edit(){
		
		$id      = $this->_get("id");
	
		if(IS_POST){
	
			$json = sendRest("BDC/Outlet/edit", $_POST, "POST");
			if($json['status'] == 1){
				wlog(1);
				$this->assign("jumpUrl",U('Site/index'));
				$this->success(L('cfg_form_tipmsg_editsucc'));
			}else{
				wlog(2,$json['info']);
				$this->error($json['info']);
			}
		}else{
	
			$model   = D("Company");
			$detail  = $model->searchOutlet($id);
			$this->assign("detail",$detail);
				
			$company = $model->search($detail['company']);
			$this->assign("company",$company);
	
			$this->display();
		}
	}
	
	/**
	 * 删除常用地址
	 */
	public function del(){
		
		$id     = $this->_param("id"    ,"intval",0);
		$map    = array("id"=>$id);
		
		$json   = sendRest("BDC/Outlet/del",$map);
		if($json['status'] == 1){
			$this->success(L('cfg_form_tipmsg_delsucc'));
		}else{
			$this->success(L('cfg_form_tipmsg_delfail'));
		}
	}
	
	/**
	 * 地图门店显示
	 */
	public function lbs(){
		
		$lang    = getLang();
		$options = getDictByKey("Dict_address",$lang);
		$this->assign("json",$options["array"]);
		
		$this->assign("pois",mapAll());
		
		$this->display();
	}
	
}
//仅供测试用
function testCurl(){
	$map['ak']  = C("MAP_AK");
	$map['geotable_id']  = C("MAP_OUTLET_ID");
	$url = "http://api.map.baidu.com/geodata/v2/column/list";
	return curlGet($url,$map);
}
?>